package com.beasts.antpage.sh.controller;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xml.sax.InputSource;

import javax.servlet.http.HttpServletRequest;
import javax.xml.rpc.ParameterMode;
import java.io.StringReader;
import java.net.URL;
import java.util.List;

/**
 * 通过webService 访问接口菜单
 * @author cn_zkn
 * @date 07/05/2018 8:39 AM
 * @since 1.0
 */
@Controller
@RequestMapping("/menu")
public class GetSubSysMenuController {

    /**
     * 二三级菜单(闻政为例)
     * @return
     */
    @RequestMapping({ "/getMenuList"})
    @ResponseBody
    public String getMenuList(HttpServletRequest req) {
        String url = req.getParameter("url");
        //方法名
        String method=req.getParameter("method");
        //年度
        String nd = req.getParameter("nd");
        String userId = req.getParameter("userId");
        String roleId = req.getParameter("roleId");

        Service service = new Service();
        String result = null;

        try {
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new URL(url));
            call.setOperationName(method);
            call.setReturnType(XMLType.XSD_STRING);
            //若请求的方法需要传入参数则在此添加参数名
            call.addParameter( "userId", XMLType.XSD_STRING,ParameterMode.IN);
            call.addParameter( "roleId", XMLType.XSD_STRING,ParameterMode.IN);
            call.addParameter( "Nd", XMLType.XSD_STRING,ParameterMode.IN);
//            // 设置被调用方法的返回值类型
            call.setReturnType(XMLType.XSD_STRING);
            result = (String) call.invoke(new Object[] {userId,roleId,nd });
            } catch (Exception e){
            e.printStackTrace();
            return null;

            }

            System.out.println(result);
        return result;
    }


    /**
     * 二三级菜单(预算为例)
     * @return
     */
    @RequestMapping({ "/getF3MenuList"})
    @ResponseBody
    public String getF3MenuList(HttpServletRequest req) {
        String url = req.getParameter("url");
        //方法名
        String method=req.getParameter("method");
        //年度
        String nd = req.getParameter("nd");
        String userId = req.getParameter("userId");
        String roleId = req.getParameter("roleId");
        String appId = req.getParameter("appId");

        Service service = new Service();
        String result = null;

        try {
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new URL(url));
            call.setOperationName(method);
            call.setReturnType(XMLType.XSD_STRING);
            //若请求的方法需要传入参数则在此添加参数名
            call.addParameter( "userId", XMLType.XSD_STRING,ParameterMode.IN);
            call.addParameter( "roleId", XMLType.XSD_STRING,ParameterMode.IN);
            call.addParameter( "appId", XMLType.XSD_STRING,ParameterMode.IN);
            call.addParameter( "Nd", XMLType.XSD_STRING,ParameterMode.IN);
//            // 设置被调用方法的返回值类型
            call.setReturnType(XMLType.XSD_STRING);
            result = (String) call.invoke(new Object[] {userId,roleId,appId,nd });
        } catch (Exception e){
            e.printStackTrace();
            return null;

        }

        System.out.println(result);
        return result;
    }


    private JSONArray toJsonArray(String xml){
        JSONArray localJSONArray = new JSONArray();
        try{   if (xml != null) {
                StringReader localStringReader = new StringReader(xml);
                InputSource localInputSource = new InputSource(localStringReader);
                SAXReader localSAXReader = new SAXReader();
                Document localDocument = localSAXReader.read(localInputSource);
                Element localElement1 = localDocument.getRootElement();
                List localList1 = localElement1.elements();
                for (int i = 0; i < localList1.size(); i++) {
                    Element localElement2 = (Element) localList1.get(i);
                    String str1 = localElement2.attributeValue("NAME");
                    String str2 = localElement2.attributeValue("DESCRIPTION");
                    List localList2 = localElement2.elements();
                    for (int j = 0; j < localList2.size(); j++) {
                        Element localElement3 = (Element) localList2.get(j);
                        String str3 = localElement3.elementTextTrim("URL");
                        String str4 = localElement3.elementTextTrim("NAME");
                        if ((str4 != null) && (!"".equals(str4))) {
                            String str5 = localElement3.elementTextTrim("AMOUNT");
                            JSONObject localJSONObject = new JSONObject();
                            localJSONObject.put("funcName", str1);
                            localJSONObject.put("funcId", "func" + i);
                            localJSONObject.put("menuSize", localList2.size());
                            localJSONObject.put("funcDesc", str2);
                            localJSONObject.put("menuUrl", str3);
                            localJSONObject.put("menuName", str4);
                            localJSONObject.put("amountNum", str5);
                            localJSONObject.put("flag", "0");
                            localJSONArray.add(localJSONObject);

                        }
                    }
                }

            }
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return localJSONArray;
    }

}
